#include "LeagueMenu.h"
#include "StringUtilities.h"
#include "PrintUtilities.h"
#include "MenuManager.h"
#include "ServiceProvider.h"
#include "GamePrintUtils.h"
#include "TeamManager.h"
#include "TeamMenu.h"
#include "LeagueManager.h"

LeagueMenu::LeagueMenu(MenuManager* const i_menuManager)
	:Menu(i_menuManager)
{
}


LeagueMenu::~LeagueMenu(void)
{
}

void LeagueMenu::Draw() const
{
	using STD::cout;
	using STD::endl;
	using namespace Utilities;

	const size_t max_char_team_name = Team::GetParams().MAX_CHAR_TEAM_NAME;

	cout << Print::Title("LEAGUE: " + m_nameLeague +  " SEASON: " + IntToStlString(m_item->m_year));
	cout << endl;

	cout << TabUpTo("POS",4);
	cout << TabUpTo("WINNER TEAM", max_char_team_name);
	cout << TabUpTo("PTS", 5);
	cout << TabUpTo("W", 5);
	cout << TabUpTo("D", 5);
	cout << TabUpTo("L", 5);
	cout << TabUpTo("GO", 5);
	cout << TabUpTo("GI", 5);
	cout << TabUpTo("GD", 5);
	cout << STD::endl;
	Print::PrintDecorationLine(cout, '-');
	cout << "\n";

	ShowLeague();

	cout << endl;

	STL::string options =   "1-"+ IntToStlString(m_item->m_result.size()) + " ) Select team for statistics.\n"+ 
							"0) Return to previous menu'.";

	cout << Print::Options(options) << "Selection: ";
}

bool LeagueMenu::Input(const STL::string& i_input)
{
	bool goodInput = true;
	const int res = Utilities::StringToInt(i_input);

	switch(res)
	{
	case 0:
		GetMM()->PopMenu();
		break;
	};

	if(res > 0 && res < static_cast<int>(m_item->m_result.size()))
	{
		TeamMenu* nextMenu = static_cast<TeamMenu*>(GetMM()->GetTeamMenu());
		nextMenu->SetTeamID(m_item->m_result[res-1].m_teamID);
		GetMM()->PushMenu(nextMenu);
	}

	return goodInput;
}


void LeagueMenu::ShowLeague() const
{
	using namespace Utilities; using STD::cout;
	const size_t max_char_team_name = Team::GetParams().MAX_CHAR_TEAM_NAME;
	for (size_t i = 0; i < m_item->m_result.size(); ++i)
	{
		int id = m_item->m_result[i].m_teamID;
		cout << TabUpTo(Utilities::IntToStlString(i+1), 4);
		cout << TabUpTo(GetSP()->GetTeamMgr().GetTeamName(id), max_char_team_name);
		cout << TabUpTo(m_item->m_result[i].m_score, 5);
		cout << TabUpTo(m_item->m_result[i].m_winMatches, 5);
		cout << TabUpTo(m_item->m_result[i].m_drawMathes, 5);
		cout << TabUpTo(m_item->m_result[i].m_lostMatches, 5);
		cout << TabUpTo(m_item->m_result[i].m_outGoals, 5);
		cout << TabUpTo(m_item->m_result[i].m_inGoals, 5);
		cout << TabUpTo(m_item->m_result[i].m_outGoals - m_item->m_result[i].m_inGoals, 5);
		cout << STD::endl;
	}
}
